#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> 
#include <wait.h>

/*
unsigned int Count=0;

void Process_Parent(void)
{
	while(1){
		Count++;
		printf("P=%d\n", Count);
		sleep(1);
	}
}

void Process_Child(void)
{
	while(1){
		Count--;
		printf("C=%d\n", Count);
		sleep(1);
	}
}
*/

int main( int argc, const char* argv[] )
{
	pid_t pid;
	int fd[2]; //fd[0]: 읽기 전용, fd[1]: 쓰기 전용
	int Message=0;
	
	if(pipe(fd) < 0){
		printf("pipe() Fail\n");
	}
	pid=fork();
	
	switch(pid){
	case -1:
		printf("fork() Fail\n");
		return -1;
	case 0:
		printf("Child Process\n");
		//Process_Child();
		while(1){
			read(fd[0], &Message, sizeof(Message));
			printf("C=%d\n", Message);
			//sleep(1);
		}		
		exit(1);
	default:
		printf("Parent Process, Child PID=%d\n", pid);
		//Process_Parent();
		while(1){
			Message++;
			printf("P=%d\n", Message);
			write(fd[1], &Message, sizeof(Message));
			sleep(1);
		}		
		wait(NULL);
	}
	
	return -1;	
}




